//
//  AXFAddressViewController.m
//  AIXF
//
//  Created by zhanghui on 2017/7/13.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFAddressViewController.h"
#import "AXFAddNewAddressViewController.h"
#import "AXFAddressManager.h"
#import "AXFMyAddressCell.h"

@interface AXFAddressViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation AXFAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化界面
    [self setupUI];
    
    //接档数据.
    _addressManager.addressList = [_addressManager addressListUnArchiver];
    
    //如果数据源中没有数据，不显示tabelView
    [self isHiddenTableView];
   
}


#pragma mark - 初始化界面
- (void)setupUI{

    //设置transform，使用transForm进行屏幕适配
    
//    self.view.transform = CGAffineTransformMakeScale(ScreenScale, ScreenScale);
    
    self.navigationItem.title = @"我的地址";
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //添加定位到当前位置按钮
    [self addLocationBtn];
    
  //创建新增地址控制器
    [self createAddNewAddressButton];
    
    //创建tbaleView
    [self setupTableView];
    
    
    //注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addressListDidChangeNotifation:) name:AddressListDidChangeNotifation object:nil];
 
}

#pragma mark - 实现通知方法
- (void)addressListDidChangeNotifation:(NSNotification *)noti{
    
    //数据源改变的时候发送通知
    [self.tableView reloadData];
    
    [self isHiddenTableView];
    
    
}



#pragma mark - 创建定位到当前位置的按钮
- (void)addLocationBtn{
    
    CGFloat locationY = KMargin + 64;
    
    UIButton *location = [self addButtonWith:@"定位到当前位置" andFrame:CGRectMake(0, locationY, ScreenWidth, MineAddressLocationButtonHeigth) andTitleColor:[UIColor blackColor] andImgName:@"v2_my_address_icon" andBgimg:nil];

    location.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:location];
}

#pragma mark - 创建新增地址按钮
- (void)createAddNewAddressButton{
    
    CGFloat width = ScreenWidth - 2* KMargin;

    UIButton *addNewAddressBtn = [self addButtonWith:@"+新增" andFrame:CGRectMake(KMargin, MineAddressAddNewsButtonY, width, MineAddressLocationButtonHeigth) andTitleColor:[UIColor whiteColor] andImgName:nil andBgimg:@"v2_my_avatar_bg"];
    
    [addNewAddressBtn addTarget:self action:@selector(AddNewAddressAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:addNewAddressBtn];
    
    
}

#pragma mark - 点击新增地址按钮的点击时间
- (void)AddNewAddressAction:(UIButton *)btn{
 
    AXFAddNewAddressViewController *add = [[AXFAddNewAddressViewController alloc]init];
    
    [self.navigationController pushViewController:add animated:YES];
    
}

#pragma mark - 创建tableVie
- (void)setupTableView{
    
    //创建tableVie
    CGFloat tableHeight = 480;
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,MyAddressTableViewY, ScreenWidth, tableHeight) style:UITableViewStylePlain];
    
    self.tableView = tableView;
    
    //设置yu ju
    tableView.rowHeight = 80;
    
    tableView.backgroundColor = [UIColor whiteColor];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    //注册cell
    [tableView registerNib:[UINib nibWithNibName:@"AXFMyAddressCell" bundle:nil] forCellReuseIdentifier:MyAddressTableViewCelldId];
    
    tableView.tableFooterView = [UIView new];
    
   
    
    [self.view addSubview:tableView];
    
    
    
}

#pragma mark - tableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _addressManager.addressList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    AXFMyAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:MyAddressTableViewCelldId forIndexPath:indexPath];
    
    
    cell.addressModel = _addressManager.addressList[indexPath.row];
    
    
    return cell;
    
}

#pragma mark - tableVie的代理方法

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewRowAction *upAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"UP" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        tableView.editing = NO;
        
        AXFMyAddressModel *model = _addressManager.addressList[indexPath.row];
        
        [_addressManager insertTopAction:model];
        
    }];

    
    
    UITableViewRowAction *deletelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        [_addressManager deleteAddressAction:indexPath.row];
    }];
    
    
    NSArray *arr = @[deletelAction,upAction];
    
    return arr;
    
}


#pragma mark - 抽取创建按钮相同的方法
- (UIButton *)addButtonWith:(NSString *)title andFrame:(CGRect)btnFrame andTitleColor:(UIColor *)btnColor andImgName:(NSString *)imgName andBgimg:(NSString *)bgImg{
    
    
    UIButton *btn = [[UIButton alloc]initWithFrame:btnFrame];
    
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    
    [btn setTitleColor:btnColor forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
  
    [btn setBackgroundImage:[UIImage imageNamed:bgImg] forState:UIControlStateNormal];
    
    
    return btn;

}

#pragma mark - 根据模型的个数判断是否显示tableView
- (void)isHiddenTableView{
    
    if (_addressManager.addressList.count > 0) {
        
        self.tableView.hidden = NO;
        
    }else{
        self.tableView.hidden = YES;
    }
}




@end
